package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.AjglCfjdsqcEntity;
import cn.com.css.taxInspection.attention.mapper.AjglCfjdsqcMapper;
import cn.com.css.taxInspection.attention.service.AjglCfjdsqcService;
import cn.com.css.taxInspection.common.exception.BusinessException;
import cn.com.css.taxInspection.gt3.service.DmGySwjgService;
import cn.com.css.taxInspection.gt3.service.DmGySwryService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Slf4j
public class AjglCfjdsqcServiceImpl extends ServiceImpl<AjglCfjdsqcMapper, AjglCfjdsqcEntity> implements AjglCfjdsqcService {

    @Resource
    private AjglCfjdsqcMapper ajglCfjdsqcMapper;
    @Resource
    private DmGySwjgService dmGySwjgService;
    @Resource
    private DmGySwryService dmGySwryService;
    @Resource
    private HttpSessionService httpSessionService;

    @Override
    @DS("oracle")
    public IPage<AjglCfjdsqcEntity> queryCfjdsTable(AjglCfjdsqcEntity ajglCfjdsqcEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String swjgDm = httpSessionService.getLogSwjgDm();
        IPage<AjglCfjdsqcEntity> page = new Page<>(ajglCfjdsqcEntity.getPage(), ajglCfjdsqcEntity.getLimit());
        LambdaQueryWrapper<AjglCfjdsqcEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AjglCfjdsqcEntity::getSwjgDm,swjgDm);
        //案件编号
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getJcajbh())) {
            queryWrapper.like(AjglCfjdsqcEntity::getJcajbh, ajglCfjdsqcEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getNsrsbh())) {
            queryWrapper.like(AjglCfjdsqcEntity::getNsrsbh, ajglCfjdsqcEntity.getNsrsbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getNsrsbhList())) {
            queryWrapper.in(AjglCfjdsqcEntity::getNsrsbh, ajglCfjdsqcEntity.getNsrsbhList());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getNsrmc())) {
            queryWrapper.like(AjglCfjdsqcEntity::getNsrmc, ajglCfjdsqcEntity.getNsrmc());
        }
        //日期
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getStartTime())) {
            queryWrapper.between(AjglCfjdsqcEntity::getWszzrq, sdf.parse(ajglCfjdsqcEntity.getStartTime()), sdf.parse(ajglCfjdsqcEntity.getEndTime()));
        }
        //销号日期
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getXhStartTime())) {
            queryWrapper.between(AjglCfjdsqcEntity::getXhrq, sdf.parse(ajglCfjdsqcEntity.getXhStartTime()), sdf.parse(ajglCfjdsqcEntity.getXhEndTime()));
        }
        queryWrapper.orderByDesc(AjglCfjdsqcEntity::getXhrq);
        IPage<AjglCfjdsqcEntity> resPage = page(page, queryWrapper);
        List<AjglCfjdsqcEntity> ajglCfjdsqcEntityList = resPage.getRecords();
        for (AjglCfjdsqcEntity cljdsqcEntity : ajglCfjdsqcEntityList) {
            cljdsqcEntity.setLrrDm(cljdsqcEntity.getLrrDm() + "|" + dmGySwryService.getSwry(cljdsqcEntity.getLrrDm()).getSwryxm());
            cljdsqcEntity.setSwjgDm(dmGySwjgService.getSwjgDmBySwjgJc(cljdsqcEntity.getSwjgDm()));
        }
        return resPage;
    }

    @Override
    @DS("oracle")
    public List<AjglCfjdsqcEntity> exportCfjdsTable(AjglCfjdsqcEntity ajglCfjdsqcEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String swjgDm = httpSessionService.getLogSwjgDm();
        LambdaQueryWrapper<AjglCfjdsqcEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AjglCfjdsqcEntity::getSwjgDm,swjgDm);
        //案件编号
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getJcajbh())) {
            queryWrapper.like(AjglCfjdsqcEntity::getJcajbh, ajglCfjdsqcEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getNsrsbh())) {
            queryWrapper.like(AjglCfjdsqcEntity::getNsrsbh, ajglCfjdsqcEntity.getNsrsbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getNsrsbhList())) {
            queryWrapper.in(AjglCfjdsqcEntity::getNsrsbh, ajglCfjdsqcEntity.getNsrsbhList());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getNsrmc())) {
            queryWrapper.like(AjglCfjdsqcEntity::getNsrmc, ajglCfjdsqcEntity.getNsrmc());
        }
        //日期
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getStartTime())) {
            queryWrapper.between(AjglCfjdsqcEntity::getWszzrq, sdf.parse(ajglCfjdsqcEntity.getStartTime()), sdf.parse(ajglCfjdsqcEntity.getEndTime()));
        }
        //销号日期
        if (TsBqUtils.isNotNull(ajglCfjdsqcEntity.getXhStartTime())) {
            queryWrapper.between(AjglCfjdsqcEntity::getXhrq, sdf.parse(ajglCfjdsqcEntity.getXhStartTime()), sdf.parse(ajglCfjdsqcEntity.getXhEndTime()));
        }
        queryWrapper.orderByDesc(AjglCfjdsqcEntity::getXhrq);
        List<AjglCfjdsqcEntity> ajglCfjdsqcEntityList = this.list(queryWrapper);
        for (AjglCfjdsqcEntity cljdsqcEntity : ajglCfjdsqcEntityList) {
            cljdsqcEntity.setLrrDm(cljdsqcEntity.getLrrDm() + "|" + dmGySwryService.getSwry(cljdsqcEntity.getLrrDm()).getSwryxm());
            cljdsqcEntity.setSwjgDm(dmGySwjgService.getSwjgDmBySwjgJc(cljdsqcEntity.getSwjgDm()));
        }
        return ajglCfjdsqcEntityList;
    }

    @Override
    @DS("oracle")
    public AjglCfjdsqcEntity queryCfjdsTableByAjbh(String jcajbh) {
        AjglCfjdsqcEntity ajglCfjdsqcEntity = ajglCfjdsqcMapper.selectByajbh(jcajbh);
        if (ajglCfjdsqcEntity == null) {
            throw new BusinessException("根据案件编号查询为空");
        }
        return ajglCfjdsqcEntity;

    }
}
